﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace InstallXSLT
{
    public class Installer
    {
        public List<String> FoundFolders;

        public void findOfficeDir()
        {
            FoundFolders = new List<string>();
            String[] folders = {@"C:\Program Files\Microsoft Office\Office13\Bibliography\Style", 
                               @"C:\Program Files (x86)\Microsoft Office\Office13\Bibliography\Style",
                               @"C:\Program Files\Microsoft Office\Office14\Bibliography\Style", 
                               @"C:\Program Files (x86)\Microsoft Office\Office14\Bibliography\Style",
                               Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Bibliography\Style"};

            String officeDir = folders[0];
            foreach (String folder in folders)
            {
                if (Directory.Exists(folder))
                {
                    FoundFolders.Add(folder);
                }
            }
        }

        public string installXslt()
        {
            try
            {
                string sourcePath = Directory.GetCurrentDirectory();
                string fileName, sourceFile, destFile;
                string[] files = System.IO.Directory.GetFiles(Directory.GetCurrentDirectory());

                foreach (string targetPath in FoundFolders)
                {
                    foreach (string file in files)
                    {
                        fileName = System.IO.Path.GetFileName(file);
                        if (fileName.EndsWith(".xsl"))
                        {
                            sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                            destFile = System.IO.Path.Combine(targetPath, fileName);
                            System.IO.File.Copy(sourceFile, destFile, true);
                        }
                    }
                }
                return "Formatvorlage erfolgreich installiert.";
            }
            catch (Exception ex)
            {
                return "Formatvorlage konnte nicht installiert werden: "+ ex.Message;
            }
        }

    }
}
